package jsonparser.core.parser;

import jsonparser.JSONObject;
import jsonparser.core.parser.nt.Pair;

/**
 * Created by pendragon on 17-5-20.
 */
public class JSONObjectBuilder implements Builder<JSONObject> {
    private JSONObject object;

    public JSONObjectBuilder(){
        object = new JSONObject();
    }

    public void buildPair(Pair pair){
        switch (pair.getValue().getType()){
            case STRING:
                object.putString(pair.getKey(), (String) pair.getValue().getValue());
                break;
            case NUMBER:
                object.putNumber(pair.getKey(), (double) pair.getValue().getValue());
                break;
            case JSONOBJECT:
                JSONObjectBuilder objectBuilder = (JSONObjectBuilder) pair.getValue().getValue();
                object.putJSONObject(pair.getKey(), objectBuilder.build());
                break;
            case JSONARRAY:
                JSONArrayBuilder arrayBuilder = (JSONArrayBuilder) pair.getValue().getValue();
                object.putJSONArray(pair.getKey(), arrayBuilder.build());
                break;
            case BOOLEAN:
                object.putBoolean(pair.getKey(), (boolean) pair.getValue().getValue());
                break;
            case NULL:
                object.putNull(pair.getKey());
                break;
        }
    }

    public JSONObject build(){
        return object;
    }
}
